package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.atguigu.lease.web.admin.vo.appointment.AppointmentQueryVo;
import com.atguigu.lease.web.admin.vo.appointment.AppointmentVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */

@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    //分页查询预约信息
    @Override
    public IPage<AppointmentVo> findAppointmentVoPage(IPage<AppointmentVo> page, AppointmentQueryVo queryVo) {
        return viewAppointmentMapper.findAppointmentVoPage(page,queryVo);
    }

    //根据ID删除租约信息
    @Override
    public void removeLeaseAgreementById(Long id) {
        //根据ID查询租约信息
        LeaseAgreement leaseAgreement = this.getById(id);
        //判断ID对应的租约信息存不存在
        if(leaseAgreement==null){
            throw new LeaseException(ResultCodeEnum.LEASE_ERROR_NULL);
        }
        //判断当前租约的状态是否为2和5
        if(LeaseStatus.SIGNED.equals(leaseAgreement.getStatus()) || LeaseStatus.WITHDRAWING.equals(leaseAgreement.getStatus())){
        //如果是则抛出异常
            throw new LeaseException(ResultCodeEnum.DELETE_LEASE_ERROR);
        }
        //如果不是则根据ID删除租约中的信息
        this.removeById(id);
    }

    //根据条件分页查询租约列表
    @Override
    public IPage<AgreementVo> findAgreementVoPage(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.findAgreementVoPage(page,queryVo);
    }

    //根据id查询租约信息
    @Override
    public AgreementVo findAgreementVoById(Long id) {
        //1、查询租约信息表LeaseAgreement
        LeaseAgreement leaseAgreement = this.getById(id);
        //2、查询签约公寓信息ApartmentInfo
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        //3、RoomInfo签约房间信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        //4、查询PaymentType支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        //5、LeaseTerm租期
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        //6、属性复制
        AgreementVo agreementVo = new AgreementVo();
        BeanUtils.copyProperties(leaseAgreement,agreementVo);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);

        return agreementVo;
    }


}




